home *** CD-ROM | disk | FTP | other *** search
- Path: news2.cais.com!news
- From: vanyo@ezaccess.net (Bill Vanyo)
- Newsgroups: comp.lang.c
- Subject: Re: WEIRD WEIRD Help Please a.s.a.p.:head pointers
- Date: Thu, 11 Apr 1996 20:16:25 GMT
- Organization: Capital Area Internet Service, Inc.
- Message-ID: <4kmh3d$kkd@news2.cais.com>
- References: <4km0vb$o04@badger.wmin.ac.uk>
- NNTP-Posting-Host: ppp-177.ezaccess.net
- X-Newsreader: Forte Free Agent 1.0.82
-
- Idoia Lertxundi <gsoec@wmin.ac.uk> wrote:
-
- >Hi C sufferers/lovers,
-
- >I am working in the C program and I have come accross a weird prob.
- >Rather than sending you code I will explain it in English as I have
- >accurately located the problem.
-
- The code would have helped enormously. I feel like I'm geussing
- without it, but think I see the problem:
-
- >I have a list which is being created dinamycally and a pointer pointing to
- >the head of the list.
-
- >ptr=AllocateSpaceforptr();
- >head=ptr;
-
- > head ptr....ptr
- > | ! ! |
- > {}-{}-{}-NULL
-
- > 1 2 3 4
-
- >When I have put data in node number 3 I allocate with a malloc call
- >node number 4 and as the function do not find suitable data to put
- >in number 4 after a for loop I assign it to NULL. i.e.
-
- It would be better not to do that malloc until you know you need it.
-
- > ptr=(FILEMODULE *)NULL;
-
- This is not affecting any part of the list pointed to by head.
-
- You should be setting the link field of the third node to NULL.
-
- My geuss is that your using only the two pointer variables head and
- ptr, with head never changing, so that to build the list you're doing
- something like
- malloc(ptr->link);
- ptr=ptr->link;
- Let's say you just added the fourth node this way. At this point the
- variable ptr and the link field of the third node are both pointing to
- the newly allocated fourth node. If you decide you want only four
- nodes, do
- ptr->link=NULL;
- If, on the other hand, you now decide you want only three nodes, you
- need to set the link field of the third node to NULL. No neat way to
- do this. Doing
- ptr=NULL;
- does not affect the link field of the third node -- this still points
- to the newly allocated fourth node.
-
- Am I geussing right?
-
- >When I do printfs I see that the list of ptr corresponds to what I was
- >expecting the above picture when I print out the content of the head list
- >which points to the begining of the ptr list the result is unexpected.
- >i.e.
-
- >head
- > |
- > {}-{}-{}-[] the last node instead of being NULL is not defined.
-
- > 1 2 3 4
-
- >By not defined I mean that is a if after malloc there has not been anything
- >put in there although I put the NULL which shows perfectly well in the ptr
- >list. Somehow the head list is not receiving the NULL asigment. WHY??
-
- >Any ideas you please mail me. I am sending this to a Newsgroup too.
-
- >Thank you.
-
- >--
- >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- >Ms Idoia Lertxundi . ,
- > .:/
- >e-mail: gsoec@wmin.ac.uk . ,,///;, ,;/
- >URL : http://www.wmin.ac.uk/~gsoec/ . o:::::::;;///
- > >::::::::;;\\\
- > `'\\\\\'" `;\
- > `;\
- >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- >"All philosophy," I told her, "is based on two things only:
- > curiosity and poor eyesight; if you had better eyesight you could see
- > perfectly well whether or not these stars are solar systems, and if
- > you were less curious you wouldn't care about knowing, which amounts
- > to the same thing. The trouble is, we want to know more than we can see."
-
- >Bernard de Fontanelle, Conversations on the Plurality of Worlds.
-
-
-
-